/**
 * User: zhangqiang
 * Date: 15-8-11
 * Time: 上午11:04
 * 查询面板
 */

define("require dojo/_base/declare dojo/_base/lang dojo/_base/array dojo/_base/fx dojo/fx/easing ../../util/ajaxUtil dojo/dom dojo/dom-attr dojo/dom-class dojo/dom-style dojo/on dojo/query dojo/topic ../CityList/weatherCodes gistech/widgets/JiXiaoDialog/JiXiaoDialog ../PlanManager/PlanManager ../BaseWidget dojo/text!./templates/Template.html".split(" "),
    function (require, declare, lang, array, baseFx,easing,ajaxUtil, dom, domAttr, domClass, domStyle, on, query, topic,weatherCodes,JiXiaoDialog, PlanManager, BaseWidget, template) {

        return declare([BaseWidget], {
            templateString: template,
            map: null,
            ismove:false,//组件位置移动判断
            constructor: function (args) {
                this.map = args.map;
                this.isShow = false;
                this.planManager = PlanManager.getInstance();
                this.widgets = {};
                this.currentCity=null;
                this.jxDg=null;
            },

            postCreate: function () {
                this.init();
                this.addEvents();
                this.inherited(arguments);
            },

            startup: function () {
                this.inherited(arguments);
            },

            destroy: function () {
                var widgets = this.widgets;
                for (var name in widgets) {
                    if (widgets[name]) {
                        widgets[name].destroy();
                    }
                }
                this.inherited(arguments);
            },

            init: function () {
                this._initCityList(this.map);
            },

            _initCityList: function (map) {
                require(["../CityList/CityList"], lang.hitch(this, function (CityList) {
                    var cityList = new CityList({
                        "map": map,
                        "responseMapExtentChange": true,
                        "supportProvince": true,
                        "initLocation": true,
                        "onDistrictChange": lang.hitch(this, this.onDistrictChange),
                        "module": this.module,
                        "position": {"l":-180, "t":40}
                    });
                    cityList.placeAt(this.cityArea);
                    cityList.startup();
                    this.widgets["cityList"]= cityList;
                }));
            },

            addEvents: function () {
            	/*监听左上方菜单收放事件*/
            	topic.subscribe("toggleNavigator2", lang.hitch(this, this.movePosition));
            	topic.subscribe("CloseJiXiaoDialog", lang.hitch(this, this.CloseJiXiaoDialog));
            	
                this.connects.push(on(this.btnCity, "click", lang.hitch(this, this.onBtnCityClick)));
                //this.connects.push(on(this.savePlanBtn, "click", lang.hitch(this, this.savePlan)));
                //this.connects.push(on(this.newPlanBtn, "click", lang.hitch(this, this.newPlan)));
                //this.connects.push(on(this.myPlanBtn, "click", lang.hitch(this, this.myPlan)));
                //this.connects.push(on(this.exportMapBtn, "click", lang.hitch(this, this.exportMap)));
                this.connects.push(on(this.jixiaoBtn, "click", lang.hitch(this, this.GetJiXiao)));
                //this.connects.push(on(this.clearBtn, "click", lang.hitch(this, this.ClearGraphics)));
                //this.connects.push(on(this.vehiclecBtn, "click", lang.hitch(this, this.ShowVehicle)));
                
                this.handlers.push(topic.subscribe("bodyClick", lang.hitch(this, this.onBodyClick)));
            },
            
            CloseJiXiaoDialog:function(){
            	this.jxDg=null;
            },
            
            GetJiXiao:function(){
            	ajaxUtil.request("../client/service/getPeopleData/getJiXiaoData","",lang.hitch(this,function(result){
            		var params = {
            			container:dojo.body(),
            			data:result,
                    }; 
            		if (this.jxDg==null){
            			this.jxDg=new JiXiaoDialog(params);
            			this.jxDg.startup();
            		}
             	}),function(err){
             		alert(err);
             	});
            },
            
            onDistrictChange: function (district) {
            	if (district.name.length<=3){
            		this.btnCity.innerHTML = district.name;
            	}else{
            		this.btnCity.innerHTML = district.name.substr(0,2)+"...";
            	}
                
               /* if (this.widgets["cityList"]) {
                    this.showCityList(false);
                }*/
                
                if (this.currentCity==district.name) return;
                else this.currentCity=district.name;
                /*var weacode="";
                for (var i = 0; i < weatherCodes.length; i++) {
                    p = weatherCodes[i];
                    if (p.cityname===district.name) {
                    	weacode=p.weacode;
                    }
                }*/
                //if (weacode=="") return;
                
                var name=district.name;
                if (district.citys){
                	name=district.citys[0].name;
                }
                if (district.name=="全国"){
                	name="北京";
                }
                var weacode=encodeURI(name);
                
                ajaxUtil.request("../client/service/clientUserController/getWeatherData","message="+weacode,lang.hitch(this,function(result){
                	/*var temp1=result.weatherinfo.temp2;
                	var temp2=result.weatherinfo.temp1;
                	var weather=result.weatherinfo.weather;
                	
                	temp1=parseInt(temp1);
                	temp2=parseInt(temp2);
                	if (weather.length>2){
                		weather=weather.substr(0,2);
                	}*/
                	
                	var temp1=result.temperature2;
                	var temp2=result.temperature1;
                	var weather=result.status1;
                	
                	temp1=parseInt(temp1);
                	temp2=parseInt(temp2);
                	if (weather.length>2){
                		weather=weather.substr(0,2);
                	}
                	this.weatherPanel.innerHTML="<span>"+temp1+"/"+temp2+"℃ "+weather+"</span>"
             	}),function(err){
             		alert(err);
             	});
            },
            
            ClearGraphics:function(evt,lName){
            	if (!lName){
            		for (var i=0;i<ioc.map.graphicLayers.length;i++){
                    	var name=ioc.map.graphicLayers[i];
                    	var gLayer=this.map.getLayer(name);
                    	for (var j = gLayer.graphics.length - 1; j >= 0; j--) {
                            var temGraphic =gLayer.graphics[j];
                            gLayer.remove(temGraphic);
                        }
                    }
            	}else{
            		var gLayer=this.map.getLayer(lName);
                	for (var i = gLayer.graphics.length - 1; i >= 0; i--) {
                        var temGraphic =gLayer.graphics[i];
                        gLayer.remove(temGraphic);
                    }
            	}
            	
            	topic.publish("ClearAllTimer", {});
            },

            /**
             * 隐藏"城市列表"面板
             * @param evt
             */
            onBodyClick: function (evt) {
                var citylistNode = this.widgets["cityList"].domNode;
                var display = domStyle.get(citylistNode, "display");
                if (display === "block") {
                    //点击对象在citys面板之外，且不是“切换城市”链接，就隐藏citys面板
                    if (!citylistNode.contains(evt.target)) {
                        //this.showCityList(false);
                    }
                }
            },

            /**
             * 切换城市列表显示
             * @param evt
             */
            onBtnCityClick: function (evt) {
                evt.stopPropagation();
                var citylistNode = this.widgets["cityList"].domNode;
                var opacity = +domStyle.get(citylistNode, "opacity");

                if (opacity === 0) {
                    this.showCityList(true);
                } else {
                    this.showCityList(false);
                }
            },

            /**
             * 显示/关闭城市列表
             * @param flag
             */
            showCityList: function (flag) {
                if (!this.widgets["cityList"]) {
                    return;
                }
                var node = this.widgets["cityList"].domNode,
                    duration = 1000;

                if (flag) {
                    domStyle.set(node, "display", "block");

                    baseFx.fadeIn({
                        node: node,
                        duration: duration
                    }).play();
                } else {
                    baseFx.fadeOut({
                        node: node,
                        duration: duration,
                        onEnd: function () {
                            domStyle.set(node, "display", "none");
                        }
                    }).play();
                }
            },

            /**
             * 保存地图
             */
            savePlan: function () {
                var isEmpty = this.planManager.isEmptyPlan();
                //空方案，弹出新建方案的窗口
                if (isEmpty) {
                    require(["../plan/PlanAdder"], lang.hitch(this, function (PlanAdder) {
                        new PlanAdder({
                            theme: "bg lightblue-bg",
                            isSave: true
                        }).startup();
                    }));
                }
                //老方案，直接保存
                else {
                    this.planManager.savePlan(lang.hitch(this, function(plan){
                    }));
                }
            },

            /**
             * 新建地图
             * @param action
             */
            newPlan: function (action) {
                require(["../plan/PlanAdder"], lang.hitch(this, function (PlanAdder) {
                    new PlanAdder({
                        theme: "bg lightblue-bg"
                    }).startup();
                }));
            },

            /**
             * 我的地图
             */
            myPlan: function () {
                require(["../MapPlan/MapPlan"], lang.hitch(this, function (Plan) {
                    new Plan({
                        theme: "bg lightblue-bg"
                    }).startup();
                }));
            },

            /**
             * 导出地图
             */
            exportMap: function () {
                topic.publish("mapExport");
            },

            /**
             * 微博分享
             */
            shareWeibo: function () {
                topic.publish("weiboShare");
            },

            /**
             * 打开数据选择器
             */
            dataList: function () {
                //topic.publish("dataSelectorOpen", {module: this.module});
            	
                require(["../DataSelectWidget/DataSelectWidget"], lang.hitch(this, function (DataSelectWidget) {
                	//获取方案图层
                	var dataIds = PlanManager.getInstance().getDataConfigs();
                	new DataSelectWidget({
                        theme: "bg lightblue-bg",
                        dataIds:dataIds
                    }).startup();
                }));
            },

            /**
             * 切换toc
             */
            tocList:function(){
                var toc = this.widgets["toc"];
                if (!toc) {
                    require(["../Toc/Toc"], lang.hitch(this, function (Toc) {
                        toc = new Toc({container:document.body});
                        toc.startup();
                        this.widgets["toc"] = toc;
                    }));
                }
                else {
                    toc.toggle();
                }
            },
            
            movePosition:function(){
            	var ef = easing["linear"];
            	var moveArgs;
            	if(this.ismove){
            		if(ef){
                        moveArgs = {
                          node: this.domNode,
                          properties: {
                    		     left: 75
                    		   } ,
                          easing: ef,
                          duration: 200
                        };
            		}
            	}else{
            		if(ef){
                        moveArgs = {
                          node: this.domNode,
                          properties: {
                    		     left: 465
                    		   } ,
                          easing: ef,
                          duration: 2
                        };
            		}
            	}
            	baseFx.animateProperty(moveArgs).play();
            	this.ismove=!this.ismove;
            	
            }
            
            //////////////////////////////////////////////////////////////////////////////
            //车辆按钮点击事件
/*            ShowVehicle:function(){
            	domClass.remove(this.vehiclecBtn, "vehicle_off");
                domClass.add(this.vehiclecBtn, "vehicle_on");
                
                topic.publish("showVehicleTree", {});
            }*/
        });
    });